<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
    <el-form-item label="consId" prop="consId" v-if="dataForm.id">
        <el-input v-model="dataForm.consId" placeholder="consId" disabled></el-input>
    </el-form-item>
    <el-form-item label="topic" prop="topic">
      <el-input v-model="dataForm.topic" placeholder="topic"></el-input>
    </el-form-item>
    <el-form-item label="消费组" prop="kafGroup">
        <el-input v-model="dataForm.kafGroup" placeholder="消费组"></el-input>
    </el-form-item>
    <el-form-item label="服务名称" prop="serviceName">
        <el-input v-model="dataForm.serviceName" placeholder="服务名称"></el-input>
    </el-form-item>
    <el-form-item label="执行handler" prop="handler">
        <el-input v-model="dataForm.handler" placeholder="执行handler"></el-input>
    </el-form-item>
    <el-form-item label="执行线程" prop="workNum">
        <el-input v-model="dataForm.workNum" placeholder="执行线程"></el-input>
    </el-form-item>
    <el-form-item label="是否启动" prop="triggerStatus">
      <el-radio-group v-model="dataForm.triggerStatus">
        <el-radio  :label=0>停止</el-radio>
        <el-radio  :label=1>启动</el-radio>
      </el-radio-group>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/bus/datakafconsumer'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          consId: 0,
          topic: '',
          kafGroup: '',
          serviceName: '',
          handler: '',
          workNum: '',
          consumptionTime: '',
          sort: '',
          delFlag: '',
          createTime: '',
          createBy: '',
          updateTime: '',
          updateBy: '',
          triggerStatus: 1
        },
        dataRule: {
          topic: [
            { required: true, message: 'topic不能为空', trigger: 'blur' }
          ],
          kafGroup: [
            { required: true, message: '消费组不能为空', trigger: 'blur' }
          ],
          serviceName: [
            { required: true, message: '服务名称不能为空', trigger: 'blur' }
          ],
          handler: [
            { required: true, message: '执行handler不能为空', trigger: 'blur' }
          ],
          workNum: [
            { required: true, message: '执行线程不能为空', trigger: 'blur' }
          ],
          triggerStatus: [
            { required: true, message: '是否启动不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            if (id) {
            getObj(id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.consId) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      },
      //重置表单
      closeDialog() {
          this.$refs["dataForm"].resetFields()
      }
    }
  }
</script>
